/*
 *
 *  Copyright 2015-2017 the original author or authors.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *
 */

package com.zwl.swagger.plugins;

import com.google.common.base.Optional;
import com.google.common.collect.Lists;
import com.zwl.swagger.CommentContainer;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.OperationBuilderPlugin;
import springfox.documentation.spi.service.contexts.OperationContext;
import springfox.documentation.spring.web.DescriptionResolver;
import springfox.documentation.swagger.common.SwaggerPluginSupport;

import java.util.List;

import static springfox.documentation.swagger.common.SwaggerPluginSupport.pluginDoesApply;

/**
 * @author zhangweilin
 * @Description:
 * @date 2021年08月20日 20:59
 */
@Component
@Order(SwaggerPluginSupport.SWAGGER_PLUGIN_ORDER)
public class OperationImplicitParametersReaderPlus implements OperationBuilderPlugin, ContextDescriper {
    private final DescriptionResolver descriptions;

    @Autowired
    CommentContainer commentContainer;

    @Autowired
    public OperationImplicitParametersReaderPlus(DescriptionResolver descriptions) {
        this.descriptions = descriptions;
    }

    @Override
    public void apply(OperationContext context) {
        context.operationBuilder().parameters(readParameters(context));
    }

    @Override
    public boolean supports(DocumentationType delimiter) {
        return pluginDoesApply(delimiter);
    }

    private List<Parameter> readParameters(OperationContext context) {
        Optional<ApiImplicitParams> annotation = context.findAnnotation(ApiImplicitParams.class);

        List<Parameter> parameters = Lists.newArrayList();
//        if (annotation.isPresent()) {
//            for (ApiImplicitParam param : annotation.get().value()) {
//                ApiImplicitParamDto apiImplicitParamDto = getApiImplicitParamDto(param, context);
//                parameters.add(OperationImplicitParameterReaderPlus.implicitParameter(descriptions, apiImplicitParamDto));
//            }
//        }

        //========以下为发现合并list有bug，比如丢失必传信息，所以先注释掉
//        String description = "";
//        try {
//            description = getMethodDescription(context);
//            MethodComment methodComment = commentContainer.getMethodCommentMap().get(description);
//            if (null != methodComment) {
//                Map<String, String> paramCommentMap = methodComment.getParamCommentMap();
//                if (!CollectionUtils.isEmpty(paramCommentMap)) {
//                    Set<Entry<String, String>> entrySet = paramCommentMap.entrySet();
//
//                    for (Entry<String, String> entry : entrySet) {
//                        String key = entry.getKey();
//                        String value = entry.getValue();
//                        ApiImplicitParamDto apiImplicitParamDto = new ApiImplicitParamDto(key, value);
//                        parameters.add(OperationImplicitParameterReaderPlus.implicitParameter(descriptions, apiImplicitParamDto));
//                    }
//                }
//            }
//        } catch (Exception e) {
//            System.err.println("OperationImplicitParametersReaderPlus异常，description: " + description);
//            e.printStackTrace();
//        }
        return parameters;
    }
}
